﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_AdminDAL : IDcms_AdminDAL
    {
        public Dcms_AdminModel AddModel(Dcms_AdminModel model)
        {
            string sQLString = "insert into Dcms_Admin(Admin_Name,Admin_Pwd,Admin_LoginTimes,Admin_LastIp,Admin_LastTime,Admin_Email,Admin_QQ,Admin_Msn,Admin_RoleId,Admin_AddTime) values(@Admin_Name,@Admin_Pwd,@Admin_LoginTimes,@Admin_LastIp,@Admin_LastTime,@Admin_Email,@Admin_QQ,@Admin_Msn,@Admin_RoleId,@Admin_AddTime) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Admin_Name", SqlDbType.VarChar), new SqlParameter("@Admin_Pwd", SqlDbType.VarChar), new SqlParameter("@Admin_LoginTimes", SqlDbType.Int), new SqlParameter("@Admin_LastIp", SqlDbType.VarChar), new SqlParameter("@Admin_LastTime", SqlDbType.DateTime), new SqlParameter("@Admin_Email", SqlDbType.VarChar), new SqlParameter("@Admin_QQ", SqlDbType.VarChar), new SqlParameter("@Admin_Msn", SqlDbType.VarChar), new SqlParameter("@Admin_RoleId", SqlDbType.Int), new SqlParameter("@Admin_AddTime", SqlDbType.DateTime) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Admin_Name;
            cmdParms[2].Value = model.Admin_Pwd;
            cmdParms[3].Value = model.Admin_LoginTimes;
            cmdParms[4].Value = model.Admin_LastIp;
            cmdParms[5].Value = model.Admin_LastTime;
            cmdParms[6].Value = model.Admin_Email;
            cmdParms[7].Value = model.Admin_QQ;
            cmdParms[8].Value = model.Admin_Msn;
            cmdParms[9].Value = model.Admin_RoleId;
            cmdParms[10].Value = model.Admin_AddTime;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Admin_Id = (int) cmdParms[0].Value;
            return model;
        }

        public Dcms_AdminModel CheckAdminLogin(string UserName, string PassWord)
        {
            string sQLString = "select * from Dcms_Admin where Admin_Name=@Admin_Name and  Admin_Pwd=@Admin_Pwd";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_Name", SqlDbType.VarChar), new SqlParameter("@Admin_Pwd", SqlDbType.VarChar) };
            cmdParms[0].Value = UserName;
            cmdParms[1].Value = PassWord;
            Dcms_AdminModel model = new Dcms_AdminModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_AdminModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public IList<Dcms_AdminModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_AdminModel> list = new List<Dcms_AdminModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_AdminModel item = new Dcms_AdminModel();
                if (ds.Tables[0].Rows[i]["Admin_Id"].ToString() != "") item.Admin_Id = int.Parse(ds.Tables[0].Rows[i]["Admin_Id"].ToString());
                item.Admin_Name = ds.Tables[0].Rows[i]["Admin_Name"].ToString();
                item.Admin_Pwd = ds.Tables[0].Rows[i]["Admin_Pwd"].ToString();
                if (ds.Tables[0].Rows[i]["Admin_LoginTimes"].ToString() != "") item.Admin_LoginTimes = int.Parse(ds.Tables[0].Rows[i]["Admin_LoginTimes"].ToString());
                item.Admin_LastIp = ds.Tables[0].Rows[i]["Admin_LastIp"].ToString();
                if (ds.Tables[0].Rows[i]["Admin_LastTime"].ToString() != "") item.Admin_LastTime = DateTime.Parse(ds.Tables[0].Rows[i]["Admin_LastTime"].ToString());
                item.Admin_Email = ds.Tables[0].Rows[i]["Admin_Email"].ToString();
                item.Admin_QQ = ds.Tables[0].Rows[i]["Admin_QQ"].ToString();
                item.Admin_Msn = ds.Tables[0].Rows[i]["Admin_Msn"].ToString();
                if (ds.Tables[0].Rows[i]["Admin_RoleId"].ToString() != "") item.Admin_RoleId = int.Parse(ds.Tables[0].Rows[i]["Admin_RoleId"].ToString());
                if (ds.Tables[0].Rows[i]["Admin_AddTime"].ToString() != "") item.Admin_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Admin_AddTime"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteAdminByRoleID(int RoleID)
        {
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Admin where Admin_Id=@Admin_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_AdminModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_AdminModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Admin where Admin_Id=@Admin_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_AdminModel model = new Dcms_AdminModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_AdminModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public Dcms_AdminModel GetModelByName(string strAdmin_Name)
        {
            string sQLString = "select * from Dcms_Admin where Admin_Name=@Admin_Name";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_Name", SqlDbType.VarChar) };
            cmdParms[0].Value = strAdmin_Name;
            Dcms_AdminModel model = new Dcms_AdminModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_AdminModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_Admin";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_AdminModel Update(Dcms_AdminModel model)
        {
            string sQLString = "update Dcms_Admin set Admin_Name=@Admin_Name,Admin_Pwd=@Admin_Pwd,Admin_LoginTimes=@Admin_LoginTimes,Admin_LastIp=@Admin_LastIp,Admin_LastTime=@Admin_LastTime,Admin_Email=@Admin_Email,Admin_QQ=@Admin_QQ,Admin_Msn=@Admin_Msn,Admin_RoleId=@Admin_RoleId,Admin_AddTime=@Admin_AddTime where Admin_Id=@Admin_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_Name", SqlDbType.VarChar), new SqlParameter("@Admin_Pwd", SqlDbType.VarChar), new SqlParameter("@Admin_LoginTimes", SqlDbType.Int), new SqlParameter("@Admin_LastIp", SqlDbType.VarChar), new SqlParameter("@Admin_LastTime", SqlDbType.DateTime), new SqlParameter("@Admin_Email", SqlDbType.VarChar), new SqlParameter("@Admin_QQ", SqlDbType.VarChar), new SqlParameter("@Admin_Msn", SqlDbType.VarChar), new SqlParameter("@Admin_RoleId", SqlDbType.Int), new SqlParameter("@Admin_AddTime", SqlDbType.DateTime), new SqlParameter("@Admin_Id", SqlDbType.Int) };
            cmdParms[0].Value = model.Admin_Name;
            cmdParms[1].Value = model.Admin_Pwd;
            cmdParms[2].Value = model.Admin_LoginTimes;
            cmdParms[3].Value = model.Admin_LastIp;
            cmdParms[4].Value = model.Admin_LastTime;
            cmdParms[5].Value = model.Admin_Email;
            cmdParms[6].Value = model.Admin_QQ;
            cmdParms[7].Value = model.Admin_Msn;
            cmdParms[8].Value = model.Admin_RoleId;
            cmdParms[9].Value = model.Admin_AddTime;
            cmdParms[10].Value = model.Admin_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public int UpdatePwd(int AdminId, string PassWord)
        {
            string sQLString = "update Dcms_Admin set Admin_Pwd=@Admin_Pwd where Admin_Id=@Admin_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_Pwd", SqlDbType.VarChar), new SqlParameter("@Admin_Id", SqlDbType.Int) };
            cmdParms[0].Value = PassWord;
            cmdParms[1].Value = AdminId;
            return SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public int UpdateRole(int AdminId, int RoleId)
        {
            string sQLString = "update Dcms_Admin set Admin_RoleId=@Admin_RoleId where Admin_Id=@Admin_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Admin_RoleId", SqlDbType.Int), new SqlParameter("@Admin_Id", SqlDbType.Int) };
            cmdParms[0].Value = RoleId;
            cmdParms[1].Value = AdminId;
            return SqlHelper.ExecuteSql(sQLString, cmdParms);
        }
    }
}

